package cn.harry.controller.wx;


import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.CommonPage;
import cn.harry.common.api.CommonResult;
import cn.harry.common.enums.BusinessType;
import cn.harry.wx.entity.CmsArticle;
import cn.harry.wx.service.CmsArticleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * CMS文章中心
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Api(tags = "wx-cmsarticle => CMS文章中心")
@RestController
@RequestMapping("wx/cmsarticle")
public class CmsArticleController {
    @Resource
    private CmsArticleService cmsArticleService;

    @ApiOperation("list => 分页获取CMS文章中心列表")
    @PreAuthorize("@ss.hasPermi('wx:cmsarticle:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<CmsArticle>> list(CmsArticle cmsArticle,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<CmsArticle> page = cmsArticleService.getPage(cmsArticle, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 查询CMS文章中心详细
     */
    @ApiOperation("{id} => 获取CMS文章中心信息")
    @PreAuthorize("@ss.hasPermi('wx:cmsarticle:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<CmsArticle> getInfo(@PathVariable Long id) {
        return CommonResult.success(cmsArticleService.selectById(id));
    }

    @ApiOperation("create => 新建CMS文章中心")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody CmsArticle cmsArticle) {
        int count = cmsArticleService.create(cmsArticle);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update/{id} => 修改指定CMS文章中心")
    @PreAuthorize("@ss.hasPermi('wx:cmsarticle:edit')")
    @SysLog(title = "CMS文章中心数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id, @RequestBody CmsArticle cmsArticle) {
        int count = cmsArticleService.update(id, cmsArticle);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("deleteByIds/{ids} => 删除指定CMS文章中心")
    @PreAuthorize("@ss.hasPermi('wx:cmsarticle:remove')")
    @SysLog(title = "CMS文章中心类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count =cmsArticleService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
